package com.xysoft.framework.mvc;

import org.apache.commons.lang3.ClassUtils;
import org.hibernate.Criteria;

import com.xysoft.framework.entity.Entity;
import com.xysoft.framework.orm.EntityManager;

public class EntityController<T> extends BasicController {
	
	private EntityManager<T> entityManager;
	
	public EntityManager<T> getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager<T> entityManager) {
		this.entityManager = entityManager;
	}

	@Override
	protected Object newCommandObject(Class<?> clazz) throws Exception {
		if(ClassUtils.isAssignable(clazz, Entity.class)) {
			return initEntity();
		} else if(ClassUtils.isAssignable(clazz, Criteria.class)) {
			return initCriteria();
		}
		return super.newCommandObject(clazz);
	}
	
	protected Object initEntity() {
		return entityManager.createNew();
	}
	
	protected Object initCriteria () {
		return entityManager.createQuery();
	}
}
